package com.shuaixin.web.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.shuaixin.web.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shuaixin.web.dao.UserDaoI;
import com.shuaixin.web.model.User;

@Component
public class UserServiceImpl implements UserServiceI {

    @Autowired
    private UserDaoI userDaoImpl;


    @Override
    public String addUser(User user) {
        Serializable uuid = userDaoImpl.save(user);
        System.out.println("userDaoImpl.save=" + user.toString());
        if (uuid == null) {
            return null;
        }
        return uuid.toString();
    }

    @Override
    public User updateUser(User user) {
        userDaoImpl.update(user);
        return getUserByuuid(user.getUuid());
    }

    @Override
    public User getUserByEmail(String email) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("email", email);
        String hql = "from User t where t.email = :email";
        return userDaoImpl.get(hql, params);
    }

    @Override
    public User getUserByEmai_Passwdl(String email, String passwd) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("email", email);
        params.put("passwd", passwd);
        String hql = "from User t where t.email = :email and t.passwd = :passwd";
        return userDaoImpl.get(hql, params);

    }

    @Override
    public User getUserByuuid(String uuid) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("uuid", uuid);
        String hql = "from User t where t.uuid = :uuid";
        return userDaoImpl.get(hql, params);
    }

    public boolean updateUUID(User user) {
        userDaoImpl.update(user);
        return true;
    }


}
